﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;
using System.Collections.Specialized;

namespace production.Controls
{
    public class prodTaskTemplateExpenseGrid : commonV2.controls.baseDataGridView
    {
        public enum ColumnNames
        {
            taskCode,accountNo, accountName, amount 
        };

        public prodTaskTemplateExpenseGrid()
        {
            this.myOnFind += new OnCellEvent(FindData);
        }

        private data.masterDS.accountDataTable myAccountTbl = new data.masterDS.accountDataTable();

        private void UpdateAccountList(string accountNo)
        {
            if (accountNo == null)
            {
                data.productionDS.prodTaskTemplateExpenseDataTable dataTbl = (data.productionDS.prodTaskTemplateExpenseDataTable)myEditDataSource.DataSource;
                for (int idx = 0; idx < dataTbl.Count; idx++)
                {
                    if (dataTbl[idx].RowState == DataRowState.Deleted) continue;
                    DataAccess.DataCache.Find(myAccountTbl, dataTbl[idx].accountNo);
                }
            }
            else DataAccess.DataCache.Find(myAccountTbl, accountNo);
        }
        
        public override void Init()
        {
            base.Init();
            data.productionDS.prodTaskTemplateExpenseDataTable dummyProdTaskTemplateExpenseTbl = new data.productionDS.prodTaskTemplateExpenseDataTable();
            DataGridViewCellStyle dataGridViewCellRatioType = new DataGridViewCellStyle();
            dataGridViewCellRatioType.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            dataGridViewCellRatioType.Format = CommonTypes.Settings.sysMaskLocalAmt;
            dataGridViewCellRatioType.NullValue = null;

            DataGridViewCellStyle dataGridViewCellCenterType = new DataGridViewCellStyle();
            dataGridViewCellCenterType.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;

            // 
            // taskCode
            // 
            commonV2.controls.DataGridViewTextBoxColumnExt taskCode = new commonV2.controls.DataGridViewTextBoxColumnExt();
            taskCode.Uppercase = true;
            taskCode.DataPropertyName = dummyProdTaskTemplateExpenseTbl.taskCodeColumn.ColumnName;
            taskCode.HeaderText = "Mã số";
            taskCode.Name = ColumnNames.taskCode.ToString();
            taskCode.Width = 130;
            taskCode.Visible = false;

            // 
            // accountNo
            // 
            

            commonV2.controls.DataGridViewTextBoxColumnExt accountNo = new commonV2.controls.DataGridViewTextBoxColumnExt();
            accountNo.Uppercase = true;
            accountNo.DataPropertyName = dummyProdTaskTemplateExpenseTbl.accountNoColumn.ColumnName;
            accountNo.HeaderText = "T.Khỏan";
            accountNo.Name = ColumnNames.accountNo.ToString();
            accountNo.Width = 90;
            // 
            // accountName
            // 

            BindingSource accountSrc = new BindingSource();
            accountSrc.DataSource = myAccountTbl;

            DataGridViewComboBoxColumn accountName = new DataGridViewComboBoxColumn();
            accountName.DataSource = accountSrc;
            accountName.DataPropertyName = dummyProdTaskTemplateExpenseTbl.accountNoColumn.ColumnName;
            accountName.DisplayMember = myAccountTbl.description1Column.ColumnName;
            accountName.ValueMember = myAccountTbl.accountNoColumn.ColumnName;
            accountName.HeaderText = "Diễn giải";
            accountName.Name = ColumnNames.accountName.ToString();
            accountName.ReadOnly = true;
            accountName.Width = 250;
            accountName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

            commonV2.ColorSet cs = commonV2.ThemeLibs.GetColorSetINFO();
            accountName.DefaultCellStyle.BackColor = cs.BackColor;
            accountName.DefaultCellStyle.ForeColor = cs.ForeColor;

            // 
            // ratio 
            // 
            DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
            amount.DataPropertyName = dummyProdTaskTemplateExpenseTbl.amountColumn.ColumnName;
            amount.DefaultCellStyle = dataGridViewCellRatioType;
            amount.HeaderText = "Đơn giá";
            amount.Name = ColumnNames.amount.ToString();
            amount.Width = 100;
            amount.Visible = true;
            // 
            // detailGrid
            // 
            this.Columns.Clear();
            this.Columns.AddRange(new DataGridViewColumn[] {
                    taskCode, accountNo, accountName,amount
                });
            this.DisableReadOnlyColumns();
        }

        protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
        {
            base.OnCellValueChanged(e);
            if (this.CurrentCell == null || !this.IsValidCell(e.RowIndex, e.ColumnIndex)) return;
            try
            {
                if (this.IsOnProcessing) return;
                this.IsOnProcessing = true;

                if (this.Columns[e.ColumnIndex].Name == ColumnNames.accountNo.ToString())
                {
                    UpdateAccountList(this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                }
                this.IsOnProcessing = false;
            }
            catch (Exception er)
            {
                this.IsOnProcessing = false;
                this.ErrorHandler(this,er);
            }
        }
        public override void Refresh()
        {
            if (IsOnProcessing) return;
            try
            {
                IsOnProcessing = true;
                UpdateAccountList(null);
                IsOnProcessing = false;
                base.Refresh();
            }
            catch (Exception er)
            {
                IsOnProcessing = false;
                ErrorHandler(this, er);
            }
        }

        private void FindData(object sender, commonV2.controls.GridEventArgs e)
        {
            FindData((DataGridView)sender, e.RowIndex, e.ColumnIndex);
        }
        private void FindData(DataGridView grid, int rowIndex, int columnIndex)
        {
            string findCode = grid.Rows[rowIndex].Cells[columnIndex].EditedFormattedValue.ToString(); ;
            if (grid.CurrentCell == null) return;
            //Code
            if (grid.Columns[columnIndex].Name.Trim() == ColumnNames.accountNo.ToString())
            {
                data.masterDS.accountRow row = application.dataFind.myAccountFind.Find(findCode, true);
                if (row != null)
                    grid.Rows[rowIndex].Cells[columnIndex].Value = row.accountNo;
                else grid.Rows[rowIndex].Cells[columnIndex].Value = DBNull.Value;
                this.myEditDataSource.ResetBindings(false);
                this.EndEdit();
            }
        }
    }
}
